package neusoft.yuejian_11.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.activity.AddfeedActivity;
import neusoft.yuejian_11.activity.InforActivity;
import neusoft.yuejian_11.activity.MypostActivity;
import neusoft.yuejian_11.activity.PrizeActivity;
import neusoft.yuejian_11.activity.RoundImageView;
import neusoft.yuejian_11.activity.UpdateInfoActivity;
import neusoft.yuejian_11.base.BaseFragment;
import neusoft.yuejian_11.bean.GetInfo;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends BaseFragment implements RetrofitListener {
    private View view=null;
    private LinearLayout  jiangpinView;
    private  ImageButton myxinxiView;
    private LinearLayout xiugaizlView;
    private  LinearLayout mytieziView;
    private LinearLayout fankuiView;
    private LinearLayout guanzhuView;
    private ImageButton zhuxiaoView;
    private TextView UserName;
    private TextView  Myyundongzhi;
    private RoundImageView touxiang;
    private   MyCollectClubFragment  myCollectClubFragment;
    private FragmentManager manager;
    private android.support.v4.app.FragmentTransaction transaction;





    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();


        jiangpinView = (LinearLayout) view.findViewById(R.id.jiangpin);
        mytieziView = (LinearLayout) view.findViewById(R.id.mytiezi);
        myxinxiView= (ImageButton) view.findViewById(R.id.myinformations);
        xiugaizlView= (LinearLayout) view.findViewById(R.id.updatezl);
        fankuiView=(LinearLayout) view.findViewById(R.id.fankui);
        UserName= (TextView) view.findViewById(R.id.update_name);
        Myyundongzhi= (TextView) view.findViewById(R.id.textView_my_yundongzhi);
        touxiang= (RoundImageView) view.findViewById(R.id.ivtouxiang);
        guanzhuView=(LinearLayout) view.findViewById(R.id.guanzhu);
        zhuxiaoView=(ImageButton) view.findViewById(R.id.zhuxiao);



//跳转到奖品列表
        jiangpinView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),PrizeActivity.class);
                startActivityForResult(intent,1 );//requestCode


            }
        });
        //跳转到消息列表
        myxinxiView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),InforActivity.class);
                startActivityForResult(intent,1 );//requestCode

            }
        });
        //跳转到修改个人信息页面
        xiugaizlView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),UpdateInfoActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });

//跳转到我的帖子列表
        mytieziView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),MypostActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });
        //跳转到反馈页面
        fankuiView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),AddfeedActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });
        //我的关注
        manager = getChildFragmentManager();
        guanzhuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCollectClubFragment = new MyCollectClubFragment();
                replace( myCollectClubFragment);
            }
        });
//        zhuxiaoView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(getContext(), LoginFragment.class);
//                new CacheInfo(getActivity()).setUserIdCache(null);
//               startActivity(intent);
//                getActivity().finish();
//
//            }
//        });


    }

    private void initData() {
        UserModel userModel=new UserModel();
        userModel.getUserInfo(getU_id(),this);
    }

    private void replace(Fragment fragment) {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.meF, fragment);
        transaction.commit();
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_my, container, false);
        return view;

    }

    @Override
    public void onSuccess(Object o, int flag) {
        GetInfo getInfo=(GetInfo) o;
        UserName.setText(getInfo.getU_name());
        Myyundongzhi.setText(getInfo.getU_yundongzhi());
        String headpic="http://10.0.2.2/yuejian14/Public/Admin/photo/"+getInfo.getU_pic();
//                Picasso.with(MyFragment.this.getActivity()).load(headpic).into(imageView);
        Picasso.with(MyFragment.this.getActivity()).load(headpic).into(touxiang);
    }

    @Override
    public void onFail() {

    }

}
